package com.dingding.service.activity.core.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Activity;
import com.dingding.common.core.beans.ActivityRelevance;
import com.dingding.common.core.util.RandomNumber;
import com.dingding.service.activity.core.dao.IActivityDao;
import com.dingding.service.activity.core.dao.IActivityRelevanceDao;

/**
 * @ClassName: ActivityRelevanceDaoImpl 
 * @Description: 兑换邀请码活动
 * @author YHQ
 * @date 2016年5月27日 下午7:45:09
 */
@Component("activityRelevanceDao")
public class ActivityRelevanceDaoImpl extends BaseDaoImpl<ActivityRelevance> implements IActivityRelevanceDao {

	@Resource(name = "activityDao")
	private IActivityDao activityDao;
	
	/**
	 * @Title: getActivityRelevance 
	 * @Description: 获取兑换邀请码信息 
	 * @param invitationCode
	 * @author YHQ 
	 * @return ActivityRelevance    返回类型
	 */
	@Override
	@Transactional(readOnly=true)
	public ActivityRelevance getActivityRelevance(String invitationCode) {
		
		// 创建查询条件
		Criterion criterion = Restrictions.eq("invitationCode", invitationCode);
		
		//邀请码活动信息
		ActivityRelevance activityRelevance = null;
		
		try {
			activityRelevance = super.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("根据用户Id和邀请码查询邀请码记录出现异常   类："+this.getClass()+" exchangeActivity()", e);
		}
		
		return activityRelevance;
	}

	/**
	 * @Title: getActivityRelevanceByUserId 
	 * @Description: 根据用户Id获取兑换邀请码 
	 * @param userId 用户ID
	 * @param activityId 活动ID
	 * @author YHQ 
	 * @return ActivityRelevance    返回类型
	 */
	@Override
	@Transactional(readOnly=true)
	public ActivityRelevance getActivityRelevanceByUserId(String userId,String activityId) {
		
		Criterion criterion = Restrictions.and(Restrictions.eq("userId", userId),Restrictions.eq("activityId", activityId));
		
		try {
			return super.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("根据用户Id获取兑换邀请码时出现异常   类："+this.getClass()+" getActivityRelevanceByUserId()", e);
		}
		
		return null;
	}

	/**
	 * @Title: addActivityRelevance 
	 * @Description: 给用户添加邀请码
	 * @param userId  用户Id
	 * @param activityId  活动Id
	 * @author YHQ 
	 * @return void    返回类型
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public ActivityRelevance addActivityRelevance(String userId,String activityId) {
		
		List<Activity> activitys = activityDao.getActivityList();
		
		ActivityRelevance relevance = new ActivityRelevance();
		
		for (Activity activity : activitys) {
			
			ActivityRelevance activityRelevance = new ActivityRelevance();
			
			activityRelevance.setUserId(userId);			//用户id
			activityRelevance.setActivityId(activity.getSnId());//活动Id
			activityRelevance.setInvitationCode("1" + RandomNumber.generateRandomNumber(6).toString());	//邀请码
			
			if(activity.getSnId().equals(activityId)){
				
				activityRelevance.setActivityNumber(1);		//参与活动次数
				activityRelevance.setIsAttendActivity(2);	//是否有效 1 未参与， 2 已参与
				
				relevance = activityRelevance;
			}else{
				
				activityRelevance.setActivityNumber(0);		//参与活动次数
				activityRelevance.setIsAttendActivity(1);	//是否有效 1 未参与， 2 已参与
			}
			
			super.save(activityRelevance);
		}
		
		return relevance;
	}

	/**
	 * @Title: updateActivityRelevance 
	 * @Description: 修改兑换邀请码信息
	 * @param activityRelevance  要修改的邀请码信息
	 * @author YHQ 
	 * @return void    返回类型
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateActivityRelevance(ActivityRelevance activityRelevance) {
		
		try {
			super.update(activityRelevance);
		} catch (Exception e) {
			log.error("修改兑换邀请码状态时出现异常  类："+this.getClass()+" updateActivityRelevance()", e);
		}
		
	}

}
